/*
 * @Date: 2025-06-18 15:29:20
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-11-06 14:27:52
 * @FilePath: \corpus\src\models\useUser.ts
 */
import { changeDept, defaultRole } from '@/services/ant-design-pro/api';
import { useModel } from '@umijs/max';
export default () => {
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};
  const getRole = (val: string[] | string) => {
    const roleArr = currentUser?.permissions ?? [];
    let arr = Array.isArray(val) ? val : [val];

    const item = roleArr?.find((item: string) => arr?.indexOf(item) > -1);
    return item;
  };
  const changeDeptCode = async function (val: string) {
    const res = await changeDept({ deptCode: val });
    if (res?.success) {
      window.location.reload();
    }
  };
  const defaultRoleCode = async function (val: string) {
    const res = await defaultRole({ roleId: val });
    if (res?.success) {
      window.location.reload();
    }
  };

  const getUserRole = (val: string[] | string) => {
    const roleArr = currentUser?.userRoles ?? [];
    let arr = Array.isArray(val) ? val : [val];

    const item = roleArr?.find((item: string) => arr?.indexOf(item) > -1);
    return item;
  };

  return {
    changeDeptCode,

    getRole,
    getUserRole,
    defaultRoleCode,
  };
};
